package per.hx.bs.sys.serviceimpl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.constant.Setting;
import per.hx.bs.sys.dao.SysNotifyRecordDao;
import per.hx.bs.entity.sys.SysNotifyRecordDO;

import java.util.*;

/**
 * @author HuXing
 * @date 2018/3/31  0:49
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping(value = "sys-service/notifyRecord")
public class SysNotifyRecordApiServiceImpl {

    private final
    SysNotifyRecordDao sysNotifyRecordDao;

    @Autowired
    public SysNotifyRecordApiServiceImpl(SysNotifyRecordDao sysNotifyRecordDao) {
        this.sysNotifyRecordDao = sysNotifyRecordDao;
    }

    /**
     * 获取通知状态
     *
     * @param id id
     * @return 通知状态
     */
    @GetMapping(value = "/get/{id}")
    public SysNotifyRecordDO get(@PathVariable("id") Long id) {
        return sysNotifyRecordDao.selectById(id);
    }

    /**
     * 获取通知状态
     *
     * @param map 条件
     * @return 通知集合
     */
    @PostMapping(value = "/list")
    public List<SysNotifyRecordDO> list(@RequestBody Map<String, Object> map) {
        return sysNotifyRecordDao.list(map);
    }

    /**
     * 按条件统计
     *
     * @param map 条件
     * @return 数量
     */
    @PostMapping(value = "/count")
    public int count(@RequestBody Map<String, Object> map) {
        return sysNotifyRecordDao.count(map);
    }


    /**
     * 保存通知状态
     *
     * @param sysNotifyRecordDO 保存通知状态
     * @return 影响行数
     */
    @PostMapping(value = "/save")
    public int save(@RequestBody SysNotifyRecordDO sysNotifyRecordDO) {
        return sysNotifyRecordDao.insert(sysNotifyRecordDO);
    }

    /**
     * 更新
     *
     * @param sysNotifyRecordDO 更新通知状态
     * @return 影响行数
     */
    @PutMapping(value = "/update")
    public int update(@RequestBody SysNotifyRecordDO sysNotifyRecordDO) {
        return sysNotifyRecordDao.updateById(sysNotifyRecordDO);
    }

    /**
     * 删除
     *
     * @param id id
     * @return 影响行数
     */
    @DeleteMapping(value = "/remove/{id}/{userId}")
    public int remove(@PathVariable("id") Long id, @PathVariable("userId") Long userId) {
        EntityWrapper<SysNotifyRecordDO> ew = new EntityWrapper<>();
        ew.setEntity(new SysNotifyRecordDO());
        ew.where("user_id = {0}", userId).andNew("notify_id ={0}", id);
        return sysNotifyRecordDao.delete(ew);
    }


    /**
     * 更改阅读状态
     *
     * @param sysNotifyRecordDO 通知状态
     * @return 影响行数
     */
    @PutMapping(value = "/changeRead")
    public int changeRead(@RequestBody SysNotifyRecordDO sysNotifyRecordDO) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("notify_id", sysNotifyRecordDO.getNotifyId());
        map.put("user_id", sysNotifyRecordDO.getUserId());
        List<SysNotifyRecordDO> notifyRecordDO = sysNotifyRecordDao.selectByMap(map);
        sysNotifyRecordDO.setId(notifyRecordDO.get(0).getId());
        return sysNotifyRecordDao.updateById(sysNotifyRecordDO);
    }

    /**
     * 批量标记为已读
     *
     * @param ids    通知id
     * @param userId 用户id
     * @return 影响行数
     */
    @GetMapping(value = "/batchRead")
    public int batchRead(@RequestParam("ids[]") Long[] ids, @RequestParam("userId") Long userId) {
        SysNotifyRecordDO sysNotifyRecordDO = new SysNotifyRecordDO();
        sysNotifyRecordDO.setIsRead(Setting.SYS_NOTIFY_READ_YES);
        sysNotifyRecordDO.setReadDate(new Date());
        EntityWrapper<SysNotifyRecordDO> ew = new EntityWrapper<>();
        ew.setEntity(new SysNotifyRecordDO());
        ew.where("user_id = {0}", userId).in("notify_id", ids);
        return sysNotifyRecordDao.update(sysNotifyRecordDO, ew);
    }

    /**
     * 批量删除
     *
     * @param ids    通知id
     * @param userId 用户id
     * @return 影响行数
     */
    @GetMapping(value = "/batchRemoveRecord")
    public int batchRemoveRecord(@RequestParam("ids[]") Long[] ids, @RequestParam("userId") Long userId) {
        EntityWrapper<SysNotifyRecordDO> ew = new EntityWrapper<>();
        ew.setEntity(new SysNotifyRecordDO());
        ew.where("user_id = {0}", userId).in("notify_id", ids);
        return sysNotifyRecordDao.delete(ew);
    }

}
